/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.bomt.service.impl;

import com.comac.ins.bomt.common.enums.BizFileTypeEnum;
import com.comac.ins.bomt.domain.BizFile;
import com.comac.ins.bomt.domain.FileExtraInfo;
import com.comac.ins.common.core.utils.BizAssertUtil;
import com.comac.ins.bomt.domain.vo.BizFileVo;
import com.comac.ins.bomt.mapper.BomtBizFileMapper;
import com.comac.ins.bomt.service.BizFileService;
import com.comac.ins.common.core.domain.model.LoginUser;
import com.comac.ins.common.json.utils.JsonUtils;
import com.comac.ins.common.mybatis.helper.BaseEntityHelper;
import com.comac.ins.system.domain.vo.SysOssVo;
import com.comac.ins.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Collections;

/**
 * Bomt文件服务实现
 *
 * @author zhanghongyi
 * @date 2024/8/2 9:20
 * @description BizFileServiceImpl
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class BizFileServiceImpl implements BizFileService {

    private final ISysOssService ossService;

    private final BomtBizFileMapper bomtBizFileMapper;

    @Override
    public BizFileVo upload(LoginUser loginUser, BizFileTypeEnum fileType, MultipartFile file, String track, String aircraftModel, String groupType, FileExtraInfo extra) {
        SysOssVo oss = ossService.upload(file);
        log.info("upload file success,fileId:{},fileName:{}", oss.getOssId(), oss.getFileName());
        return insertFileRecord(loginUser, fileType, oss, track, aircraftModel, groupType, extra);
    }

    @Override
    public BizFileVo upload(LoginUser loginUser, BizFileTypeEnum fileType, File file, String fileName, String track, String aircraftModel, String groupType, FileExtraInfo extra) {
        SysOssVo oss = ossService.upload(file, fileName);
        log.info("upload file success,fileId:{},fileName:{}", oss.getOssId(), oss.getFileName());
        return insertFileRecord(loginUser, fileType, oss, track, aircraftModel, groupType, extra);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(LoginUser loginUser, Long fileId) {
        ossService.deleteWithValidByIds(Collections.singletonList(fileId), true);
        bomtBizFileMapper.deleteById(fileId);
        log.info("delete file success,fileId:{}", fileId);
    }

    private BizFileVo insertFileRecord(LoginUser loginUser, BizFileTypeEnum fileType, SysOssVo oss, String track, String aircraftModel, String groupType, FileExtraInfo extra) {
        BizFile bizFile = new BizFile();
        bizFile.setAircraftModel(aircraftModel);
        bizFile.setFileId(oss.getOssId());
        bizFile.setFileName(oss.getFileName());
        bizFile.setOriginalName(oss.getOriginalName());
        BaseEntityHelper.fillCreateByAndUpdateBy(bizFile, loginUser);
        bizFile.setFileType(fileType.getCode());
        bizFile.setGroupType(groupType);
        bizFile.setTrack(track);
        if (extra != null) {
            bizFile.setExtraInfo(JsonUtils.toJsonString(extra));
        }
        int insertCnt = bomtBizFileMapper.insert(bizFile);
        BizAssertUtil.isTrue(insertCnt == 1, "insert file record fail");
        return bomtBizFileMapper.selectVoById(oss.getOssId(), BizFileVo.class);
    }
}
